Fix:Error message when creating the same page for different applications#1797
Fix:Error message when creating the same page for different applications#1797hexqi merged 2 commits intoopentiny:developfrom
Conversation
WalkthroughModified the pages service to enforce route uniqueness through a pre-insert existence check rather than a database index constraint. Added route defaulting logic that falls back to page name or "Untitled" when route is not provided. Adjusted the unique index from Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
mockServer/src/services/pages.js (2)
34-37: Redundant unique index on_id.NeDB automatically enforces uniqueness on
_idby default. This explicit index declaration has no effect and can be removed.♻️ Suggested fix
- this.db.ensureIndex({ - fieldName: '_id', - unique: true - })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@mockServer/src/services/pages.js` around lines 34 - 37, Remove the redundant explicit unique index creation on `_id` by deleting the this.db.ensureIndex({ fieldName: '_id', unique: true }) call in the pages service (where this.db is initialized). NeDB enforces uniqueness for `_id` automatically, so remove that ensureIndex invocation (or guard it with a comment if you want to retain historical context) and keep any other index setup intact.
111-120: Consider adding route conflict validation toupdateas well.The
createmethod now validates route uniqueness per app, butupdatedoes not. If a page's route is updated to conflict with another page in the same app, the duplicate will be allowed.For consistency, consider adding similar validation when
params.routeis being changed.♻️ Suggested fix
async update(id, params) { const updateData = { ...params } + + if (updateData.route) { + const current = await this.db.findOneAsync({ _id: id }) + if (current) { + const existing = await this.db.findOneAsync({ + app: current.app.toString(), + route: updateData.route, + _id: { $ne: id } + }) + if (existing) { + return getResponseData(null, { + code: 'ROUTE_CONFLICT', + message: `Route "${updateData.route}" already exists in app "${current.app}"` + }) + } + } + } + if (updateData.page_content && typeof updateData.page_content === 'object') { updateData.page_content = JSON.stringify(updateData.page_content) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@mockServer/src/services/pages.js` around lines 111 - 120, The update method (async update(id, params)) needs the same route-uniqueness check as create: when params.route is provided and differs from the current page's route, query the DB for any other document with the same app and route (e.g., this.db.findOneAsync({ app: existing.app, route: params.route, _id: { $ne: id } })); if such a document exists, throw/return a conflict error instead of proceeding to this.db.updateAsync; otherwise proceed to stringify page_content as currently done, update, and return getResponseData(parsePageContent(result)).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@mockServer/src/services/pages.js`:
- Around line 92-98: The getResponseData helper currently ignores an
error.status so the 409 from pages.js is never applied; update getResponseData
(function getResponseData in mockServer/src/tool/Common.js) to read and return a
status field from the error object (alongside code and message) so callers like
the return in mockServer/src/services/pages.js that call getResponseData(...)
receive the status and the HTTP layer can use it, or alternatively set
ctx.status (or the equivalent HTTP response status) in the pages.js controller
before returning getResponseData; prefer modifying getResponseData to include
status for consistent handling across callers.
---
Nitpick comments:
In `@mockServer/src/services/pages.js`:
- Around line 34-37: Remove the redundant explicit unique index creation on
`_id` by deleting the this.db.ensureIndex({ fieldName: '_id', unique: true })
call in the pages service (where this.db is initialized). NeDB enforces
uniqueness for `_id` automatically, so remove that ensureIndex invocation (or
guard it with a comment if you want to retain historical context) and keep any
other index setup intact.
- Around line 111-120: The update method (async update(id, params)) needs the
same route-uniqueness check as create: when params.route is provided and differs
from the current page's route, query the DB for any other document with the same
app and route (e.g., this.db.findOneAsync({ app: existing.app, route:
params.route, _id: { $ne: id } })); if such a document exists, throw/return a
conflict error instead of proceeding to this.db.updateAsync; otherwise proceed
to stringify page_content as currently done, update, and return
getResponseData(parsePageContent(result)).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: ade700fe-9126-4641-bc48-70d49e2cd193
⛔ Files ignored due to path filters (1)
mockServer/src/database/pages.dbis excluded by!**/*.db
📒 Files selected for processing (1)
mockServer/src/services/pages.js
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
Bug Fixes